let timer = null;
const api = require('../../../utils/interface.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths: {
      home: "",
      deputy: ""
    },
    isShow: false,
    plateNumber: "请输入并确认您的车牌号",
    keyBoardType: 1,
    params: null,
    tempFilePaths2: "",
    name:""
  },
  onLoad: function (options) {
    this.data.params = JSON.parse(options.params)
    this.setData({
      name:this.data.params.name
    })
    if (this.data.params.carNumber){
      this.data.tempFilePaths.home = this.data.params.picPositiveUrl
      this.data.tempFilePaths.deputy = this.data.params.picBackUrl
      this.setData({
        tempFilePaths: this.data.tempFilePaths,
        plateNumber: this.data.params.carNumber
      })
    }
    console.log(this.data.params)
  },
  chooseImg(e) {
    let index = e.currentTarget.dataset.index;
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: "正在上传",
          mask: true
        })
        that.uoLoad(res.tempFilePaths[0], "file").then(res => {
          if (index == 1) {
            that.data.tempFilePaths.home = res
            that.setData({
              tempFilePaths: that.data.tempFilePaths
            })
          } else if (index == 2) {
            that.data.tempFilePaths.deputy = res
            that.setData({
              tempFilePaths: that.data.tempFilePaths
            })
          }

        })

      }
    })

  },
  inputOk(e) {
    this.hideGB()
    this.setData({
      isShow: false,
      keyBoardType: 1
    })
  },
  inputChange(e) {
    if (!e.detail) {
      return false
    }
    if (this.data.plateNumber.length >= 8) {
      return false
    }
    console.log(e.detail)
    this.data.plateNumber += e.detail
    this.setData({
      keyBoardType: 2,
      plateNumber: this.data.plateNumber
    })

  },
  inputdelete(e) {
    console.log(1)
    this.data.plateNumber = this.data.plateNumber.substring(0, this.data.plateNumber.length - 1)
    this.setData({
      plateNumber: this.data.plateNumber
    })
    if (!this.data.plateNumber.length) {
      this.setData({
        keyBoardType: 1
      })
    }
  },
  exit() {
    this.hideGB()
    this.setData({
      isShow: false,
    })
  },
  showKey() {
    if (this.data.plateNumber = "请输入车牌号") {
      this.setData({
        plateNumber: ""
      })
    }
    this.setData({
      isShow: true
    })

    this.showGb()
  },
  showGb() {
    let _this = this
    clearInterval(timer)
    timer = setInterval(() => {
      _this.setData({
        gbDtate: !_this.data.gbDtate
      })
    }, 800)
  },
  hideGB() {
    clearInterval(timer)
    this.setData({
      gbDtate: false
    })
  },
  uoLoad(file, name) {
    return new Promise((resolve, reject) => {
      var jwt = wx.getStorageSync('jwt') ? wx.getStorageSync('jwt') : ''
      wx.uploadFile({
        url: api.uploadFile, //仅为示例，非真实的接口地址
        filePath: file,
        name: name,
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          'type': 1,
          'version': 1.0,
          'jwt': jwt
        },
        success(res) {

          wx.hideLoading()
          let json = JSON.parse(res.data)
          if (json.status == 200) {
            resolve(json.result)
          } else {
            wx.showModal({
              title: '提示',
              content: json.message,
            })
          }
        },
        fail(err) {
          console.log(err)
        },
      })
    })

  },
  submit() {
    if (!this.data.tempFilePaths.home) {
      wx.showModal({
        title: '提示',
        content: '请上传平台截图',
        showCancel: true
      })
      return false
    }
    if (!this.data.tempFilePaths.deputy) {
      wx.showModal({
        title: '提示',
        content: '请上传平台截图',
        showCancel: true
      })
      return false
    }
    if (this.data.plateNumber == "请输入车牌号" || !this.data.plateNumber) {
      wx.showModal({
        title: '提示',
        content: '请填写车牌号码',
        showCancel: true
      })
      return false
    }
    if (this.data.plateNumber.length<6) {
      wx.showModal({
        title: '提示',
        content: '请填写车牌号码',
        showCancel: true
      })
      return false
    }
    
    let user = wx.getStorageSync('userInfo')
    let data = {
      memberId: user.id,
      carNumber: this.data.plateNumber,
      picPositiveUrl: this.data.tempFilePaths.home,
      picBackUrl: this.data.tempFilePaths.deputy,
      certificationType: this.data.params.index,
      sourceType: this.data.params.ridx
    }
    api.certification(data).then(res => {
      console.log(res.data.message)
      if (res.data.status == 200) {
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel: false,
          success() {
            wx.switchTab({
              url: '/pages/person/person',
            })
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel: true,
        })
      }
    })
  }
})